package models;

import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;

import entities.GoogleAnalytic;

public class GoogleAnalyticModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "ga_";
	
	private int limit = 500;
	
	private static GoogleAnalyticModel instance = null;
	
	public static GoogleAnalyticModel getInstance() {
		if (instance == null) {
			instance = new GoogleAnalyticModel();
		}
		
		return instance;
	}
	
	private GoogleAnalyticModel() {
		try {
			ObjectifyService.register(GoogleAnalytic.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		/*try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }*/
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public void insert(GoogleAnalytic analytic) {
		if (cache != null) {
			cache.put(prefix + analytic.getAnanyticAccount(), analytic);
		}
		ofy.async().put(analytic);
	}
	
	public GoogleAnalytic get(String googleAnalyticId) {
		GoogleAnalytic analytic = new GoogleAnalytic();
		
		if (cache != null && cache.containsKey(prefix + googleAnalyticId)) {
			analytic = (GoogleAnalytic) cache.get(prefix + googleAnalyticId);
		} else {
			try {
				analytic = ofy.get(new Key<GoogleAnalytic>(GoogleAnalytic.class, googleAnalyticId));
				if (cache != null) {
					cache.put(prefix + googleAnalyticId, analytic);
				}
			} catch (Exception e) {
				analytic = null;
			}
		}
		return analytic;
	}
	
	public void add(String googleAnalyticId, List<String> links) {
		boolean isChange = false;
		GoogleAnalytic analytic = get(googleAnalyticId);
		if (analytic == null) {
			analytic = new GoogleAnalytic();
			analytic.setAnanyticAccount(googleAnalyticId);
		}
		
		List<String> domains = analytic.getDomains();
		for (int i = 0; i < links.size(); i++) {
			if (!domains.contains(links.get(i)) && domains.size() <= limit) {
				isChange = true;
				domains.add(links.get(i));
			}
		}
		
		analytic.setDomains(domains);
		
		if (isChange) {
			if (cache != null) {
				cache.put(prefix + googleAnalyticId, analytic);
			}
			insert(analytic);
		}
	}
	
	public void insert(String googleAnalyticId, String domain) {
		boolean isChange = false;
		
		GoogleAnalytic analytic = get(googleAnalyticId);
		if (analytic == null) {
			analytic = new GoogleAnalytic();
			analytic.setAnanyticAccount(googleAnalyticId);
		}
		
		List<String> domains = analytic.getDomains();
		
		if (!domains.contains(domain) && domains.size() <= limit) {
			isChange = true;
			domains.add(domain);
		}
		
		analytic.setDomains(domains);
		
		if (isChange) {
			if (cache != null) {
				cache.put(prefix + googleAnalyticId, analytic);
			}
			insert(analytic);
		}
	}
	
	public void delete(GoogleAnalytic analytic) {
		if (cache != null && cache.containsKey(prefix + analytic.getAnanyticAccount())) {
			cache.remove(prefix + analytic.getAnanyticAccount());
		}
		ofy.delete(analytic);
	}
}
